package com.longcai.piaojia.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.activity.MyOrderDetailActivity;
import com.longcai.piaojia.adapter.TicketOrderListAdapter;
import com.longcai.piaojia.conn.MyOrderListGet;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/8/4.
 */
public class FragmentTicketOrder extends BaseFragment implements View.OnClickListener {

    @BoundView(R.id.fragment_ticket_order_bepay_tv) private TextView fragment_ticket_order_bepay_tv;
    @BoundView(R.id.fragment_ticket_order_beused_tv) private TextView fragment_ticket_order_beused_tv;
    @BoundView(R.id.fragment_ticket_order_evaluated_tv) private TextView fragment_ticket_order_evaluated_tv;
    @BoundView(R.id.fragment_ticket_order_refunds_tv)  private TextView fragment_ticket_order_refunds_tv;
    @BoundView(R.id.fragment_ticket_order_lv) private PullToRefreshListView pullToRefreshListView;
    private ListView fragment_ticket_order_lv;
    private List<MyOrderListGet.MyOrderEntity> lists = new ArrayList<>();
    private TicketOrderListAdapter ticketOrderListAdapter;
    private SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");
    private int state = 0;
    private MyOrderListGet.MyOrderListEntity myOrderList;

    private void setLastUpdateTime() {
        String text = formatDateTime(System.currentTimeMillis());
        pullToRefreshListView.setLastUpdatedLabel(text);
    }

    private String formatDateTime(long time) {
        if (0 == time) {
            return "";
        }

        return mDateFormat.format(new Date(time));
    }

    private MyOrderListGet myOrderListGet = new MyOrderListGet(new AsyCallBack<MyOrderListGet.MyOrderListEntity>(){

        @Override
        public void onEnd(String toast, int type) throws Exception {
            super.onEnd(toast, type);

            pullToRefreshListView.onPullDownRefreshComplete();
            pullToRefreshListView.onPullUpRefreshComplete();

        }

        @Override
        public void onSuccess(String toast, int type, MyOrderListGet.MyOrderListEntity myOrderListEntity) throws Exception {
            super.onSuccess(toast, type, myOrderListEntity);

            myOrderList = myOrderListEntity;

            lists.addAll(myOrderListEntity.data);
            ticketOrderListAdapter.notifyDataSetChanged(state);

            if(type==0){

                fragment_ticket_order_lv.setSelection(0);

            }

            setLastUpdateTime();


        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(getActivity(), toast, Toast.LENGTH_LONG).show();

        }

    });

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = BoundViewHelper.boundView(this, inflater.inflate(R.layout.fragment_ticket_order, null));

        pullToRefreshListView.setPullRefreshEnabled(true);
        pullToRefreshListView.setPullLoadEnabled(true);
        pullToRefreshListView.setScrollLoadEnabled(false);

        fragment_ticket_order_lv = pullToRefreshListView.getRefreshableView();

//        fragment_ticket_order_lv.setDivider(getActivity().getResources().getDrawable(R.color.background_bg_color));
//        fragment_ticket_order_lv.setDividerHeight(ScaleScreenHelperFactory.getInstance().getWidthHeight(18));
        fragment_ticket_order_lv.setDivider(null);

        fragment_ticket_order_lv.setAdapter(ticketOrderListAdapter = new TicketOrderListAdapter(getContext(), lists));

        fragment_ticket_order_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(getActivity(), MyOrderDetailActivity.class);

                intent.putExtra("order_number", lists.get(position).order_number);

                startActivity(intent);

            }
        });

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                lists.clear();
                myOrderListGet.page = 1;
                myOrderListGet.user_id = BaseApplication.BasePreferences.readUserId();
                myOrderListGet.status = state;
                myOrderListGet.execute(getActivity(), false, 0);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if (myOrderList != null && myOrderList.total != lists.size()) {

                    myOrderListGet.page = myOrderList.current_page + 1;
                    myOrderListGet.user_id = BaseApplication.BasePreferences.readUserId();
                    myOrderListGet.status = state;
                    myOrderListGet.execute(getActivity(), false, 1);

                } else {

                    pullToRefreshListView.onPullUpRefreshComplete();

                    pullToRefreshListView.onPullDownRefreshComplete();

                }

            }

        });

        setLastUpdateTime();

        fragment_ticket_order_bepay_tv.setOnClickListener(this);
        fragment_ticket_order_beused_tv.setOnClickListener(this);
        fragment_ticket_order_evaluated_tv.setOnClickListener(this);
        fragment_ticket_order_refunds_tv.setOnClickListener(this);

        fragment_ticket_order_bepay_tv.performClick();

        myOrderListGet.user_id = BaseApplication.BasePreferences.readUserId();
        myOrderListGet.status = state;
        myOrderListGet.page = 1;
        myOrderListGet.execute(getActivity());

        setAppCallBack(new RefreshOrderList());

        return view;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.fragment_ticket_order_bepay_tv:
                initColor();
                fragment_ticket_order_bepay_tv.setTextColor(getActivity().getResources().getColor(R.color.theme_color));

                if(state == 0){

                    return;

                }

                state = 0;

                lists.clear();

                myOrderListGet.user_id = BaseApplication.BasePreferences.readUserId();
                myOrderListGet.status = state;
                myOrderListGet.page = 1;
                myOrderListGet.execute(getActivity());

                break;

            case R.id.fragment_ticket_order_beused_tv:
                initColor();
                fragment_ticket_order_beused_tv.setTextColor(getActivity().getResources().getColor(R.color.theme_color));

                if(state == 1){

                    return;

                }

                state = 1;

                lists.clear();

                myOrderListGet.user_id = BaseApplication.BasePreferences.readUserId();
                myOrderListGet.status = state;
                myOrderListGet.page = 1;
                myOrderListGet.execute(getActivity());

                break;

            case R.id.fragment_ticket_order_evaluated_tv:
                initColor();
                fragment_ticket_order_evaluated_tv.setTextColor(getActivity().getResources().getColor(R.color.theme_color));

                if(state == 2){

                    return;

                }

                state = 2;

                lists.clear();

                myOrderListGet.user_id = BaseApplication.BasePreferences.readUserId();
                myOrderListGet.status = state;
                myOrderListGet.page = 1;
                myOrderListGet.execute(getActivity());

                break;

            case R.id.fragment_ticket_order_refunds_tv:
                initColor();
                fragment_ticket_order_refunds_tv.setTextColor(getActivity().getResources().getColor(R.color.theme_color));

                if(state == 6){

                    return;

                }

                state = 6;

                lists.clear();

                myOrderListGet.user_id = BaseApplication.BasePreferences.readUserId();
                myOrderListGet.status = state;
                myOrderListGet.page = 1;
                myOrderListGet.execute(getActivity());

                break;

        }

    }

    private void initColor() {
        fragment_ticket_order_bepay_tv.setTextColor(getActivity().getResources().getColor(R.color.text_color));
        fragment_ticket_order_beused_tv.setTextColor(getActivity().getResources().getColor(R.color.text_color));
        fragment_ticket_order_evaluated_tv.setTextColor(getActivity().getResources().getColor(R.color.text_color));
        fragment_ticket_order_refunds_tv.setTextColor(getActivity().getResources().getColor(R.color.text_color));
    }

    public class RefreshOrderList implements AppCallBack {

        public void refresh() {

            lists.clear();
            myOrderListGet.page = 1;
            myOrderListGet.user_id = BaseApplication.BasePreferences.readUserId();
            myOrderListGet.status = state;
            myOrderListGet.execute(getActivity(), false, 1);
        }
    }



}
